package com.motorhome.manage.controller;

import com.motorhome.base.dao.Pager;
import com.motorhome.base.easyui.EasyuiDataGridResult;
import com.motorhome.base.util.Result;
import com.motorhome.manage.domain.Car;
import com.motorhome.manage.service.CarService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;

/**
 * @author: John
 */
@Controller
@RequestMapping("/car")
public class CarController {

    @Autowired
    private CarService carService;

    @RequestMapping("/index")
    public String index(HttpServletRequest request) {
        return "car/index";
    }

    @RequestMapping(value = "/getCarList", method = RequestMethod.POST)
    public @ResponseBody String getCarList(HttpServletRequest request) {
        String page = request.getParameter("page");
        String rows = request.getParameter("rows");
        Pager<Car> pager = carService.getCarList(page, rows);
        return new EasyuiDataGridResult<>(pager.getTotalCount(), pager.getItems()).toJson();
    }

    @RequestMapping(value = "/addCar", method = RequestMethod.POST)
    public @ResponseBody Result addCar(HttpServletRequest request, MultipartFile file) {
        String name = request.getParameter("name");
        String description = request.getParameter("description");
        String viewUrl = request.getParameter("viewUrl");
        String status = request.getParameter("status");
        String remark = request.getParameter("remark");
        String insurance = request.getParameter("insurance");
        String carNum = request.getParameter("carNum");
        String flag = request.getParameter("flag");
        return carService.addCar(name, description, status, remark, file, viewUrl, insurance, carNum,flag);
    }

    @RequestMapping(value = "/updateCar", method = RequestMethod.POST)
    public @ResponseBody Result updateCar(HttpServletRequest request, MultipartFile file) {
        String id = request.getParameter("id");
        String name = request.getParameter("name");
        String description = request.getParameter("description");
        String viewUrl = request.getParameter("viewUrl");
        String status = request.getParameter("status");
        String remark = request.getParameter("remark");
        String insurance = request.getParameter("insurance");
        String carNum = request.getParameter("carNum");
        String flag = request.getParameter("flag");
        return carService.updateCar(id, name, description, status, remark, file, viewUrl, insurance, carNum,flag);
    }

    @RequestMapping(value = "/deleteCar", method = RequestMethod.POST)
    public @ResponseBody Result deleteCar(HttpServletRequest request) {
        String id = request.getParameter("id");
        return carService.deleteCar(id);
    }

    @RequestMapping(value = "/addPrice", method = RequestMethod.POST)
    public @ResponseBody Result addPrice(HttpServletRequest request) {
        String carId = request.getParameter("carId");
        String priceDesc = request.getParameter("priceDesc");
        String price = request.getParameter("price");
        String orderBy = request.getParameter("orderBy");
        String rateWeekend = request.getParameter("rateWeekend");
        String rate1 = request.getParameter("rate1");
        String day1 = request.getParameter("day1");
        String rate2 = request.getParameter("rate2");
        String day2 = request.getParameter("day2");
        String rate3 = request.getParameter("rate3");
        String day3 = request.getParameter("day3");
        return carService.addPrice(request, carId, priceDesc, price, orderBy, rateWeekend, rate1, day1, rate2, day2, rate3, day3);
    }

    @RequestMapping(value = "/deletePrice", method = RequestMethod.POST)
    public @ResponseBody Result deletePrice(HttpServletRequest request) {
        String id = request.getParameter("id");
        return carService.deletePrice(id);
    }

    @RequestMapping(value = "/bindDevice", method = RequestMethod.POST)
    public @ResponseBody Result bindDevice(HttpServletRequest request) {
        String carId = request.getParameter("id");
        String deviceId = request.getParameter("deviceId");
        String deviceNumber = request.getParameter("deviceNumber");
        String deviceName = request.getParameter("deviceName");
        return carService.bindDevice(carId, deviceId, deviceNumber, deviceName);
    }

}
